package com.sicheng.metadata;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.*;

/**
 * @author zsc
 */

public class DrawCar {
    public static int speed = 6;
    public static int startSpeed = speed;

    public static void main(String[] args) {
        JFrame frame1 = new JFrame();
        TrayIcon trayIcon;
        // 检查系统托盘是否支持
        if (!SystemTray.isSupported()) {
            System.out.println("系统托盘不支持");
            return;
        }

        // 1.创建托盘图标
        Image image = Toolkit.getDefaultToolkit().getImage("src/com/atguigu/-v/car.png");

        // 2.停留提示text
        String text = "停留此处提示";

        // 创建弹出菜单
        // final PopupMenu popup = createPopupMenu();
        PopupMenu popup = new PopupMenu();
        MenuItem recoverItem = new MenuItem("reset");
        MenuItem closeItem = new MenuItem("quit");
        popup.add(recoverItem);
        popup.add(closeItem);

        trayIcon = new TrayIcon(image, text, popup);
        final SystemTray systemTray = SystemTray.getSystemTray();

        trayIcon.setPopupMenu(popup);

        //定义ActionListener监听器
        //监听二级菜单的操作
        TrayIcon finalTrayIcon = trayIcon;
        ActionListener MenuListen = e -> {
            if (e.getActionCommand().equals("quit")) {
                systemTray.remove(finalTrayIcon);
                System.exit(0);
            } else if (e.getActionCommand().equals("reset")) {
                frame1.setExtendedState(JFrame.NORMAL);
                frame1.setVisible(true);
            }

        };
        //为弹出菜单项添加监听器
        recoverItem.addActionListener(MenuListen);
        closeItem.addActionListener(MenuListen);

        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    frame1.add(popup);
                    popup.show(frame1, e.getXOnScreen(), e.getYOnScreen());
                }
            }
        });


        try {
            systemTray.add(trayIcon);//设置托盘的图标
        } catch (AWTException e2) {
            e2.printStackTrace();
        }

        frame1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame1.addWindowListener(
                new WindowAdapter() {
                    public void windowIconified(WindowEvent e) {
                        frame1.dispose();//窗口最小化时dispose该窗口
                    }
                });
        trayIcon.addMouseListener(
                new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        if (e.getButton() == MouseEvent.BUTTON1)//单击击托盘窗口再现
                            frame1.setExtendedState(Frame.NORMAL);
                        frame1.setVisible(true);
                    }
                });

        frame1.setSize(700, 700);
        MyPanel panel = new MyPanel();
        frame1.add(panel);
        JButton btnSlow = new JButton("低速");
        JButton btnQuick = new JButton("高速");
        JButton btnStartSpeed = new JButton("原速");
        JButton speedup = new JButton("加速");
        JButton slowdown = new JButton("减速");
        btnQuick.setBounds(600, 0, 80, 80);
        btnSlow.setBounds(600, 80, 80, 80);
        btnStartSpeed.setBounds(600, 160, 80, 80);
        speedup.setBounds(600, 240, 80, 80);
        slowdown.setBounds(600, 320, 80, 80);
        panel.setLayout(null);

        // 添加按钮
        panel.add(btnSlow);
        panel.add(btnQuick);
        panel.add(btnStartSpeed);
        panel.add(speedup);
        panel.add(slowdown);
        btnSlow.addActionListener(e -> {
            System.out.println("低速按钮被点击");
            speed = 2;
        });
        btnQuick.addActionListener(e -> {
            // 获取到的事件源就是按钮本身
            System.out.println("高速按钮被点击");
            speed = 10;
        });
        btnStartSpeed.addActionListener(e -> {
            // 获取到的事件源就是按钮本身
            System.out.println("起始速度按钮被点击");
            speed = startSpeed;
        });
        speedup.addActionListener(e -> {
            // 获取到的事件源就是按钮本身
            System.out.println("加速");
            speed++;
        });
        slowdown.addActionListener(e -> {
            // 获取到的事件源就是按钮本身
            System.out.println("减速");
            if (speed != 0)
                speed--;
        });

        frame1.setContentPane(panel);
        frame1.setVisible(true);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future future = executor.submit(new Thread(panel));

        try {
            future.get(1, TimeUnit.SECONDS);
            // Set the time out of the prime no. search task
            //            executor.shutdown();
            System.out.println("执行future get");
        } catch (TimeoutException e) {
            executor.shutdown();
        } catch (InterruptedException | ExecutionException e) {
            executor.shutdown();
            e.printStackTrace();
        }
        executor.shutdownNow();
    }
}

class MyPanel extends JPanel implements Runnable {
    int x = 10, y = 10;
    Toolkit t = Toolkit.getDefaultToolkit();

    public void paint(Graphics g) {
        super.paint(g);
        Image i = t.getImage("src/com/atguigu/-v/car.png");
        g.drawImage(i, x, y, this);
    }

    @Override
    public void run() {
        while (!Thread.interrupted()) {
            x = x + DrawCar.speed;
            y = y + DrawCar.speed;
            if (x > 710) {
                x = 0;
            }
            if (y > 710) {
                y = 0;
            }
            try {
                Thread.sleep(20);
            } catch (Exception e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
}
